﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace bigormscreensaver
{
    public partial class ScreensaverForm : Form
    {
        #region Init, mode and general
        private bool bIsInPreviewMode = false;
        private Point pMouseLocation;
        public Settings settings;

        public ScreensaverForm(Settings settings)
        {
            this.settings = settings;
            InitializeComponent();
        }

        public ScreensaverForm(Settings settings, Rectangle Bounds)
            : this(settings)
        {
            this.Bounds = Bounds;
        }

        public ScreensaverForm(Settings settings, IntPtr pWndParent)
            : this(settings)
        {
            Util.SetParent(this.Handle, pWndParent); // Set the preview window as the parent of this window
            Util.SetWindowLong(this.Handle, -16, new IntPtr(Util.GetWindowLong(this.Handle, -16) | 0x40000000)); // Make this a child window so it will close when the parent dialog closes
            // Place window inside the parent
            Rectangle ParentRect;
            Util.GetClientRect(pWndParent, out ParentRect);
            Size = ParentRect.Size;
            Location = new Point(0, 0);
            bIsInPreviewMode = true;
        }


        public bool bModeImage = false;
        public bool bModeLinkedin = false;
        public bool bModeNetwork = false;
        public bool bModeFeed = false;
        public bool bModeText = false;

        private void ScreensaverForm_Load(object sender, EventArgs e)
        {
            if (!bIsInPreviewMode)
            {
                Cursor.Hide();
                TopMost = true;
            }
            switch (settings.Mode_SelectedIndex)
            { 
                case 0: bModeImage = true; break;
                case 1: bModeLinkedin = true; break;
                case 2: bModeNetwork = true; break;
                case 3: bModeFeed = true; break;
                case 4: bModeText = true; break;
            }

            Image_Load(sender, e);
            Linkedin_Load(sender, e);
            Network_Load(sender, e);
            Feed_Load(sender, e);
            Text_Load(sender, e);
        }

        private void ScreensaverForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!bIsInPreviewMode)
                Cursor.Show();
        }

        private void ScreensaverForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (!bIsInPreviewMode && image_groupBox.Visible == false)
            {
                if (!pMouseLocation.IsEmpty) // Terminate if mouse is moved a significant distance
                    if (Math.Abs(pMouseLocation.X - e.X) + Math.Abs(pMouseLocation.Y - e.Y) > 5)
                        Application.Exit();
                pMouseLocation = e.Location; // Update current mouse location
            }
        }

        private void ScreensaverForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (bModeImage)
                Image_KeyDown(sender, e);
            else if (bModeLinkedin)
                Linkedin_KeyDown(sender, e);
            else if (bModeNetwork)
                Network_KeyDown(sender, e);
            else if (bModeFeed)
                Feed_KeyDown(sender, e);
            else if (bModeText)
                Text_KeyDown(sender, e);
        }

        private void Feed_KeyDown(object sender, KeyEventArgs e)
        {
            TopMost = false;
            Close();
            throw new NotImplementedException();
        }

        private void Linkedin_KeyDown(object sender, KeyEventArgs e)
        {
            TopMost = false;
            Close();
            throw new NotImplementedException();
        }

        private void ScreensaverForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.ToLower(e.KeyChar) == 'c')
                configureToolStripMenuItem_Click(sender, new EventArgs());
            else
                if (!bIsInPreviewMode)
                    Application.Exit();
        }

        private void ScreensaverForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Right)
                if (!bIsInPreviewMode)
                    Application.Exit();
        }

        private void configureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!bIsInPreviewMode)
            {
                Cursor.Show();
                TopMost = false;
            }
            new SettingsForm(settings).ShowDialog();
            if (!bIsInPreviewMode)
            {
                Cursor.Hide();
                TopMost = true;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (!bIsInPreviewMode)
            {
                Cursor.Show();
                TopMost = false;
            }
        }

        private void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            if (!bIsInPreviewMode)
            {
                Cursor.Hide();
                TopMost = true;
                pMouseLocation = new Point();
            }
        }
        #endregion Init, mode and general
        #region Image

        private Settings.CImagesEnumerator _settings_ImageEnumerator = null;
        public Settings.CImagesEnumerator settings_ImageEnumerator
        {
            get
            {
                if (_settings_ImageEnumerator == null)
                    _settings_ImageEnumerator = (Settings.CImagesEnumerator)settings.Images.GetEnumerator();
                return _settings_ImageEnumerator;
            }
        }
        private void Image_ShowNextImage()
        {
            image_pictureBox.InitialImage = image_pictureBox.Image;
            if (settings_ImageEnumerator.MoveNext())
                try
                {
                    image_pictureBox.LoadAsync(settings_ImageEnumerator.Current.ToString());
                }
                catch (Exception)
                {
                    try { image_pictureBox.LoadAsync(settings.Images.Default.ToString()); }
                    catch (Exception) { }
                }
            else
                try { image_pictureBox.LoadAsync(settings.Images.Default.ToString()); }
                catch (Exception) { }
        }

        private void Image_ShowPreviousImage()
        {
            image_pictureBox.InitialImage = image_pictureBox.Image;
            if (settings_ImageEnumerator.MovePrevious())
                try
                {
                    image_pictureBox.LoadAsync(settings_ImageEnumerator.Current.ToString());
                }
                catch (Exception)
                {
                    try { image_pictureBox.LoadAsync(settings.Images.Default.ToString()); }
                    catch (Exception) { }
                }
            else
                try { image_pictureBox.LoadAsync(settings.Images.Default.ToString()); }
                catch (Exception) { }
        }

        public void Image_Show(bool bVisible = true)
        {
            image_Timer.Enabled = false;
            image_pictureBox.Visible = bVisible;
            if (bVisible)
            { // Load initial image
                this.BackColor = settings.Image_Backcolor;
                image_Timer.Interval = settings.Image_IntervalForTimer;
                Image_ShowNextImage();
                image_Timer.Enabled = true; // Start timer
            }
        }

        private void Image_Load(object sender, EventArgs e)
        {
            if (bModeImage)
                Image_Show();
        }

        private void image_Timer_Tick(object sender, EventArgs e)
        {
            Image_ShowNextImage();
        }

        private void Image_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyValue)
            {
                case 39:
                    foreach (var form in Application.OpenForms)
                        if (form is ScreensaverForm)
                        {
                            ScreensaverForm sf = ((ScreensaverForm)form);
                            sf.Image_ShowNextImage(); // Right
                        }
                    break;
                case 37: 
                    foreach (var form in Application.OpenForms)
                        if (form is ScreensaverForm)
                        {
                            ScreensaverForm sf = ((ScreensaverForm)form);
                            sf.Image_ShowPreviousImage(); // Left
                        }
                    break;
                case 38:
                    foreach (var form in Application.OpenForms)
                        if (form is ScreensaverForm)
                        {
                            ScreensaverForm sf = ((ScreensaverForm)form);
                            sf.image_groupBox.Show(); // Up
                        }
                    TopMost = false;
                    Cursor.Show();
                    break;
                case 40:
                    foreach (var form in Application.OpenForms)
                        if (form is ScreensaverForm)
                        {
                            ScreensaverForm sf = ((ScreensaverForm)form);
                            sf.image_groupBox.Hide(); 
                        }
                    TopMost = true;
                    Cursor.Hide();
                    break;
                default:
                    Debug.WriteLine("Image_KeyDown " + e.KeyValue.ToString());
                    break;
            }
        }

        private void image_pictureBox_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            image_url_linkLabel.Text = image_pictureBox.ImageLocation;
            if (settings_ImageEnumerator == null || settings_ImageEnumerator.CurrentLocator == null)
                image_searchPath_linkLabel.Text = "";
            else
                image_searchPath_linkLabel.Text = settings_ImageEnumerator.CurrentLocator.FindSpec;
        }

        private void image_linkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (sender is LinkLabel)
            {
                ProcessStartInfo psi = new ProcessStartInfo(((LinkLabel)sender).Text);
                psi.UseShellExecute = true;
                image_pictureBox.Focus();
                Process p = Process.Start(psi);
            }
        } 

        #endregion Image
        #region Linkedin
        public void Linkedin_Show(bool bVisible = true)
        {
            linkedin_webBrowser.Visible = bVisible;
            if (bVisible)
            {
                linkedin_webBrowser.Navigate("http://www.linkedin.com");
            }
        }
        private void Linkedin_Load(object sender, EventArgs e)
        {
            if (bModeLinkedin)
                Linkedin_Show();
        }
        #endregion Linkedin
        #region Feed
        public void Feed_Show(bool bVisible = true)
        {
            feed_pictureBox.Visible = bVisible;
            if (bVisible)
            {
            }
        }
        private void Feed_Load(object sender, EventArgs e)
        {
            if (bModeFeed)
                Feed_Show();
        }
        #endregion Feed
        #region Network
        public void Network_Show(bool bVisible = true)
        {
            network_pictureBox.Visible = bVisible;
            if (bVisible)
            {
            }
        }
        private void Network_Load(object sender, EventArgs e)
        {
            if (bModeNetwork)
                Network_Show();
        }
        private void Network_KeyDown(object sender, KeyEventArgs e)
        {
            TopMost = false;
            Close();
            throw new NotImplementedException();
        }
        #endregion Network
        #region Text
        public void Text_Show(bool bVisible = true)
        {
            text_Timer.Enabled = false;
            textLabel.Visible = bVisible;
            if (bVisible)
            {
                textLabel.Text = settings.Text_Caption;
                textLabel.Font = bIsInPreviewMode ? settings.Text_FontForPreview : settings.Text_Font;
                textLabel.ForeColor = settings.Text_Color;
                textLabel.BackColor = settings.Text_Backcolor;
                this.BackColor = settings.Text_Backcolor;
                text_Timer.Interval = settings.Text_IntervalForTimer;
            }
            text_Timer.Enabled = bVisible;
        }
        private void Text_Load(object sender, EventArgs e)
        {
            if (bModeText)
                Text_Show();
        }
        private void Text_TimerTick(object sender, EventArgs e)
        {
            textLabel.Left = settings.Text_rand.Next(Math.Max(1, Bounds.Width - textLabel.Width));
            textLabel.Top = settings.Text_rand.Next(Math.Max(1, Bounds.Height - textLabel.Height));
            textLabel.Text = settings.Text_Caption;
        }
        private void Text_KeyDown(object sender, KeyEventArgs e)
        {
            TopMost = false;
            throw new NotImplementedException();
        }
        #endregion Text

    }
}
